package com.hmyd.hmyd_android.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.CreditListAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.MyScore;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.dto.Score;
import com.hmyd.hmyd_android.shop.JfMallActivity;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 我的积分
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_my_score)
public class MyScoreActivity extends BasicListActivity {
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.score_list)
	private XListView mList;
	@ViewInject(R.id.total)
	private TextView total;
	@ViewInject(R.id.goto_score_mall)
	private Button goto_score_mall;
	@ViewInject(R.id.total_label)
	private TextView total_label;

	private final int REFRESH = 1;
	private final int LOADMORE = 2;
	private final int GETMYSCORE = 3;
	protected boolean hasMore = false;
	private String cursorId;

	private List<MyScore> listData = new ArrayList<MyScore>();
	private CreditListAdapter mCreditListAdapter;

	public XListView getXList() {
		return mList;
	}
	
	@OnClick({R.id.goto_score_mall})
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.goto_score_mall:
			Intent intent = new Intent(this,JfMallActivity.class);
			AppBasic.getInstance().finishCurrentActivity();
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!isLoading && listData.size() == 0) {
			onRequest(REFRESH, UrlConstant.SCORERECORDLIST, HttpMethod.GET, null);
			onRequest(GETMYSCORE, UrlConstant.GETMYSCORE, HttpMethod.GET, null);
		}
	}

	@Override
	public void onPostSuccess(int postId, String result) {
		super.onPostSuccess(postId, result);

		Gson gson = new Gson();

		if (postId == GETMYSCORE) {
			java.lang.reflect.Type type2 = new TypeToken<Score>() {
			}.getType();
			Score score = gson.fromJson(result, type2);

			total.setText(Utils.amountConversionFormat(score.score));
			AppBasic.totalScore = score.score;

			return;
		}

		java.lang.reflect.Type type2 = new TypeToken<Page<MyScore>>() {
		}.getType();
		Page<MyScore> page = gson.fromJson(result, type2);
		hasMore = page.hasMore;

		if (page != null && page.records != null) {
			switch (postId) {
			case REFRESH:
				listData.clear();
				listData.addAll(page.records);
				break;
			case LOADMORE:
				listData.addAll(page.records);
				break;
			default:
				break;
			}

			if (listData.size() > 0) {
				cursorId = listData.get(listData.size() - 1).cursorId;
			} else {
				cursorId = null;
			}

			mCreditListAdapter.notifyDataSetChanged();
		}

		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}

	}

	@Override
	public void onPostFailure(int postId, String msg) {
		super.onPostFailure(postId, msg);
		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}

	}

	@Override
	protected void setParams() {
		home_title_txt.setText("我的积分");
		home_title.setOnTouchListener(touch);
		
		mList.setPullRefreshEnable(true); // 允许下拉刷新
		mList.setPullLoadEnable(false); // 禁止加载更多
		mList.setAutoLoadEnable(false); // 禁止自动加载
		mList.setXListViewListener(this);// 加载监听
		
		mCreditListAdapter = new CreditListAdapter(this, listData);
		mList.setAdapter(mCreditListAdapter);
		
		total_label.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction() & MotionEvent.ACTION_MASK) {
				case MotionEvent.ACTION_DOWN:
					goto_score_mall.setVisibility(View.INVISIBLE);
					break;
				case MotionEvent.ACTION_UP:
					goto_score_mall.setVisibility(View.VISIBLE);
					break;
				default:
					break;
				}
				return true;
			}
		});
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onRefresh() {
		super.onRefresh();

		onRequest(REFRESH, UrlConstant.SCORERECORDLIST, HttpMethod.GET, null);
		onRequest(GETMYSCORE, UrlConstant.GETMYSCORE, HttpMethod.GET, null);
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();

		RequestParams params = new RequestParams();
		params.addBodyParameter("lastCursor", cursorId);

		onRequest(LOADMORE, UrlConstant.SCORERECORDLIST, HttpMethod.GET, params);
	}

}
